package day17.homework2;

import lombok.SneakyThrows;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UdpServer {
    @SneakyThrows
    public static void main(String[] args) {
        try (
                OutputStream os = new FileOutputStream("d:\\a.jpg");
                DatagramSocket datagramSocket = new DatagramSocket(1000);
        ) {
            byte[] data = new byte[1024];
            // 数据包
            DatagramPacket datagramPacket = new DatagramPacket(data, data.length);
            while (true) {
                // 接收数据
                datagramSocket.receive(datagramPacket);
                os.write(datagramPacket.getData(), 0, datagramPacket.getLength());
                if (datagramPacket.getLength() != data.length) break;
            }
        }
    }
}
